#ifndef __RECORD_SERVER_H__
#define __RECORD_SERVER_H__

#include <map>
#include <mutex>
#include <functional>
#include "Util/util.h"
#include "jsoncpp/json.h"
#include "Thread/Thread.h"
#include "Fdisk/fdisk.h"
#include "Fdisk/Storage.h"
#include "StreamPushServer.h"

using namespace Fdt;
using namespace std;


class recordServer
{
public:
	using ConfigFunc = std::function<bool(Json::Value &, int &result)>;

	static recordServer *instance()
	{
		if(s_recordServer == nullptr)
		{
			s_recordServer = new recordServer();
		}
		return s_recordServer;
	}

	~recordServer(){}

	void RegistRPC();

	bool recordStart(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool recordStop(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool recordList(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool recordGetSd(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool recordFormatSd(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool recordStatus(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);

private:
	void initPushers(bool enableRecord, bool allChannel = false);
	void ThreadProc(Fdt::Thread *thread);
protected:
	recordServer();
private:
	static recordServer *			s_recordServer;
	std::recursive_mutex			m_mutex;
	std::string 			        mountDir = "/mnt/sd";
	int needFormatCard = 0;
	int partNum        = 0;
	int mountFlag = 0; //0 -- unmount 1 -- mount
	StorageDevice 					*pStorage = NULL;
    StorageInfo stStorageInfo;
    StoragePartitionInfo stStoragePartitionInfo;

	std::map<std::string, std::map<void *, ConfigFunc>> _setMap;
	std::map<std::string, std::map<void *, ConfigFunc>> _verifyMap;

	Fdt::Thread 						_thread{"RecordOps"};

	bool m_is_recording = false;
    std::vector<std::string> m_push_urls; 
};
#endif
